$(function(){
	$("#navbar1 ul li a[href^='#']").on('click', function(e) {

		   // prevent default anchor click behavior
		   e.preventDefault();

		   // store hash
		   var hash = this.hash;

		   // animate
		   $('html, body').animate({
		       scrollTop: $(this.hash).offset().top
		     }, 300, function(){

		       // when done, add hash to url
		       // (default click behaviour)
		       window.location.hash = hash;
		     });

		});
	
	$(".go-down").on('click',function(e){
		e.preventDefault();
		$('html,body').animate({
			scrollTop: $('#newsletter').offset().top-80
		}, 300, function(){
			window.location.hash = "#newsletter";
		});
	});
	
	
	//Newsletter Button
	$("#news_btn").on('click',function(){
		$(this).fadeOut(300,function(){
			$("#show_news_input").fadeIn(300);
		});
	});
	
	
	
	//Newsletter email saving to db
	function isValidEmailAddress(emailAddress) {
	    var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
	    return pattern.test(emailAddress);
	};
	
	$("#send_nl").on('click',function(){
		var name = $(".nl_name").val();
		var email = $(".nl_email").val();
		
		if( name=="" || name==null || email=="" || email==null || !isValidEmailAddress( email ) ) { 
			alert("Empty inputs/invalid emails, are not accepted. Please retry.");
		}
		else{
			var dataStr = "name="+name+"&email="+email;
			$.ajax({
			  type: "GET",
			  url: "newsletter.php",
			  data: dataStr
			})
			  .done(function( msg ) {
			    alert( msg );
			    $("#show_news_input").fadeOut(300,function(){
					$("#news_btn").fadeIn(300);
				});
			  });
		}
		

		
	});
	
	
	
});